package com.ysd.salepromotion.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.DoubleSerializer;
import com.alibaba.fastjson.serializer.FloatCodec;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

public class JsonUtils {
	private static final SimplePropertyPreFilter FILTER = new SimplePropertyPreFilter();
	private static final SerializerFeature[] FEATURES = { SerializerFeature.DisableCircularReferenceDetect };

	static {
		FILTER.getExcludes().add("schema");
		SerializeConfig.getGlobalInstance().put(Float.class, new FloatCodec("0.0######"));
		SerializeConfig.getGlobalInstance().put(Double.class, new DoubleSerializer("0.0######"));
	}

	public static String toJSONString(Object object) {
		if (object == null){
			return "";
		}
		return JSON.toJSONString(object, FILTER, FEATURES);
	}

	public static <T> T copyObject(Object srcObject, Class<T> clazz){
		String jsonString = JsonUtils.toJSONString(srcObject);
		return JSONObject.parseObject(jsonString, clazz);
	}

	public static Object toObject(String strJson){
		return JSON.parseObject(strJson);
	}
}